import os
import shutil
import time

print('*************************************************')
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print('Start, please wait...')

dir_C = os.getcwd()

def C2I():
    print('**********C2I**********')
    files_yadan = os.listdir(dir_C + '\\file_c')

    path = '.\\file_i'

    if(os.path.exists(path)):
        pass
    else:
        os.system('mkdir ' + path)

    path_yadan = '.\\file_i\\yadan_i'

    if(os.path.exists(path_yadan)):
        pass
    else:
        os.system('mkdir ' + path_yadan)


    for file_yadan in files_yadan:
        tmp = file_yadan.split('.')
        if(len(tmp) == 1):
            pass
        elif(tmp[1].lower() == 'c'):
            print('riscv-none-embed-gcc -march=rv32im -mabi=ilp32 -E -o ' + '.\\file_i\\yadan_i\\' + tmp[0] + '.i ' + '.\\file_c\\' + file_yadan)
            os.system('riscv-none-embed-gcc -march=rv32im -mabi=ilp32 -E -o ' + '.\\file_i\\yadan_i\\' + tmp[0] + '.i ' + '.\\file_c\\' + file_yadan)


def I2S():
    print('**********I2S**********')
    files_yadan = os.listdir(dir_C + '\\file_i\\yadan_i')

    path = '.\\file_s'

    if(os.path.exists(path)):
        pass
    else:
        os.system('mkdir ' + path)

    path_yadan = '.\\file_s\\yadan_s'

    if(os.path.exists(path_yadan)):
        pass
    else:
        os.system('mkdir ' + path_yadan)


    shutil.copy('.\\file_s\\crt0_riscv_E.S',path_yadan)
    # shutil.copy('.\\file_s\\pulpino.h',path_yadan)
    #shutil.copy('.\\yadan\\file_s\\yadan.h',path_yadan)


    for file_yadan in files_yadan:
        tmp = file_yadan.split('.')
        if(len(tmp) == 1):
            pass 
        elif(tmp[1].lower() == 'i'):
            print('riscv-none-embed-gcc -march=rv32im -mabi=ilp32 -S -o ' + '.\\file_s\\yadan_s\\' + tmp[0] + '.s ' + '.\\file_i\\yadan_i\\' + file_yadan)
            os.system('riscv-none-embed-gcc -march=rv32im -mabi=ilp32 -S -o ' + '.\\file_s\\yadan_s\\' + tmp[0] + '.s ' + '.\\file_i\\yadan_i\\' + file_yadan)

    
def S2O():
    print('**********S2O**********')
    files_yadan = os.listdir(dir_C + '\\file_s\\yadan_s')

    path = '.\\file_o'

    if(os.path.exists(path)):
        pass
    else:
        os.system('mkdir ' + path)

    path_yadan = '.\\file_o\\yadan_o'

    if(os.path.exists(path_yadan)):
        pass
    else:
        os.system('mkdir ' + path_yadan)


    for file_yadan in files_yadan:
        tmp = file_yadan.split('.')
        if(len(tmp) == 1):
            pass
        elif(tmp[1].lower() == 's'):
            print('riscv-none-embed-gcc -march=rv32im -mabi=ilp32 -c -o ' + '.\\file_o\\yadan_o\\' + tmp[0] + '.o ' + '.\\file_s\\yadan_s\\' + file_yadan)
            os.system('riscv-none-embed-gcc -march=rv32im -mabi=ilp32 -c -o ' + '.\\file_o\\yadan_o\\' + tmp[0] + '.o ' + '.\\file_s\\yadan_s\\' + file_yadan)


def Ld():
    #cmd = 'riscv-none-embed-gcc -static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles --std=gnu99 -T link.ld '
    cmd = 'riscv-none-embed-ld -T link.ld '

    path_yadan = dir_C + '\\file_o\\yadan_o'

    files_yadan = os.listdir(path_yadan)

    for file_yadan in files_yadan:
        tmp = file_yadan.split('.')
        if(len(tmp) == 2 and tmp[1] == 'o'):
            cmd = cmd + '.\\file_o\\yadan_o\\' + file_yadan+ ' '
        else:
            pass
    
    
    
    cmd = cmd + 'libc.a -o yadan'#  libgcc.a
    print('**********link**********')
    print(cmd)
    os.system(cmd)
    print('riscv-none-embed-objcopy -O ihex yadan simple.hex')
    os.system('riscv-none-embed-objcopy -O ihex yadan simple.hex')
    print('riscv-none-embed-objcopy -O binary yadan simple.bin')
    os.system('riscv-none-embed-objcopy -O binary yadan simple.bin')


C2I()
I2S()
S2O()
Ld()
print('**********final process**********')
print('riscv-none-embed-objdump -D yadan > obj.s')
os.system('riscv-none-embed-objdump -D yadan > obj.s')

# print('python hex2hex2.py')
# os.system('python hex2hex2.py')
print('Done!')